package rpg.visual.window;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.event.MouseEvent;

import pgui.PVisual;
import pgui.visual.PComponent;
import rpg.visual.VisualImage;

public class VisualWindowHeader extends PComponent {
	private String title;

	// Creation
	public VisualWindowHeader(PVisual visual, String title) {
		super(visual);
		this.setTitle(title);
		
		this.setRelativeWidth(100);
		this.setHeight(26);
		this.setClip(true);
	}

	// Mouse Events
	@Override
	public void onMousePressed(MouseEvent event) {
		super.onMousePressed(event);
		mouseLock(true);
	}
	
	@Override
	public void onMouseReleased(MouseEvent event) {
		super.onMouseReleased(event);
		mouseLock(false);
	}
	
	@Override
	public void onMouseDragged(MouseEvent event, MouseEvent lastEvent) {
		super.onMouseDragged(event, lastEvent);
		if(!mouseLocked())
			return;
		
		int translateX = event.getX() - lastEvent.getX();
		this.getParent().setX(this.getParent().getX() + translateX);
		
		int translateY = event.getY() - lastEvent.getY();
		this.getParent().setY(this.getParent().getY() + translateY);
	}
	
	// Paint Events
	@Override
	public void onPaint(Graphics2D g) {
		super.onPaint(g);

		g.setColor(new Color(35, 30, 0));
		VisualImage.opacity(g, 0.07f);
		g.fillRect(0, 1, this.getLastWidth(), getLastHeight());

		VisualImage.opacity(g, 1f);
		g.setColor(Color.darkGray);
		g.setFont(g.getFont().deriveFont(Font.BOLD));
		g.drawString(this.getTitle(), 6, 20);
	}

	// Get & Set
	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}
}
